package com.codingday.app;

import java.util.ArrayList;

import android.app.TabActivity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TabHost;
import android.widget.Toast;

import com.google.ads.*;

public class GeoAlarm extends TabActivity {
	public static Context context;
	GeoAlarm geoAlarmList;
	TabHost mTabHost;
	static GeoAlarm instance=null;
	
	private static ServiceConnection mConnection;
	private static GeoAlarmService mService;
	
	AdView adView;
	

	public GeoAlarmService getService(){
		if(mService == null){
			doConnectToGeoAlarmService();
		}
		return mService;
	}
	
	public boolean doConnectToGeoAlarmService(){
		// Connect to Service
		mConnection = new ServiceConnection(){

			@Override
			public void onServiceConnected(ComponentName name, IBinder service) {
				mService = ((GeoAlarmService.GeoAlarmBinder)service).getService();
				Log.i("GeoAlarm","onServiceConnected");
			}

			@Override
			public void onServiceDisconnected(ComponentName name) {
				mService = null;
				Log.i("GeoAlarm","onServiceDisonnected");
			}
			
		};
		boolean isSuccess = bindService(new Intent(this,GeoAlarmService.class), mConnection, 0);
		Log.i("GeoAlarm","Serivce Connect : "+ isSuccess);
		return isSuccess;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		
		GeoAlarm.context = getApplicationContext();
		
		setContentView(R.layout.tabview);
		mTabHost = getTabHost();

		// new
        mTabHost.addTab(mTabHost.newTabSpec("tab_todonew")
        		//.setIndicator(null,getResources().getDrawable(R.drawable.listicon))
        		.setIndicator("", getResources().getDrawable(R.drawable.ic_menu_add))
        		.setContent(new Intent(this, TODONew.class))
        		//.setContent(R.id.tabview2)
        );
		
        mTabHost.addTab(mTabHost.newTabSpec("tab_todolist")
        		//.setIndicator(null,getResources().getDrawable(R.drawable.listicon))
        		.setIndicator("", getResources().getDrawable(R.drawable.ic_menu_agenda))
        		.setContent(new Intent(this, TODOListView.class))
        		//.setContent(R.id.tabview2)
        );
        
        /*
        mTabHost.addTab(mTabHost.newTabSpec("tab_search")
        		.setIndicator("Search")
        		//.setContent(new Intent(this, TODOSearch.class))
        		.setContent(R.id.tabview2)
        		);
         */
        mTabHost.addTab(mTabHost.newTabSpec("tab_todomap")
        		//.setIndicator(null,getResources().getDrawable(R.drawable.mapicon))
        		.setIndicator("", getResources().getDrawable(R.drawable.ic_menu_mapmode))
        		.setContent(new Intent(this, TODOMapView.class))
        		//.setContent(R.id.tabview3)
        );
        
        // adjust tabhost height
        for(int i = 0 ; i < mTabHost.getTabWidget().getChildCount() ; i++){
        	mTabHost.getTabWidget().getChildAt(i).getLayoutParams().height = 70;
        }
        
        
        // Start Service Here
        if(GeoAlarmService.isInstanceCreated() == false){
	        Log.i("GeoAlarm","Trying To Start Service");
	        Intent serviceIntent = new Intent(context,GeoAlarmService.class);
	        if(startService(serviceIntent) == null){
	        	Log.i("GeoAlarm","Service start Failed");
	        }else{
	        	Log.i("GeoAlarm","Service start Successed");
	        }
	         
	        Log.i("GeoAlarm","After Trying to Start Service");
        }else{
        	Log.i("GeoAlarm","Service is Already Running");
        }
        

        
        instance = this;
        
        // AdMob
        /*
        AdManager. setTestDevices( new String[] {
        		AdManager.TEST_EMULATOR,          // Android emulator
        		"PHONE_ID",                                   // Phone ID
        		} );
        		AdView adView = (AdView)findViewById(R.id.ad);
        		adView.requestFreshAd();
        */
        
        // Admob Publisher ID : a14df5c73c08cb1
		adView = (AdView)this.findViewById(R.id.adView);

	}
	

	
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		//unConnectToGeoAlarmService();
		adView.stopLoading();
	}


	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
    	doConnectToGeoAlarmService();
	    adView.loadAd(new AdRequest());
	}


	public static GeoAlarm getInstance(){
		if(instance == null){
			instance = new GeoAlarm();
		}
		return instance;
	}
	
	
	public static Context getContext(){
		return context;
	}
	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
    	boolean result = super.onCreateOptionsMenu(menu);
    	
    	MenuInflater menuInflator = new MenuInflater(this);
    	//menuInflator.inflate(R.layout.main_menu, menu);
    	menuInflator.inflate(R.menu.main_menu, menu);
    	return result;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		boolean result =  super.onOptionsItemSelected(item);
		
		switch(item.getItemId())
		{
			case R.id.MENU_ABOUT:
				Intent aboutIntent = new Intent(this, About.class);
				startActivity(aboutIntent);
			break;
			/*
			case R.id.MENU_DEBUG:
				Intent DebugIntent = new Intent(this, TODONowList.class);
				DebugIntent.putExtra("fromNotification", false);
				ArrayList<Integer> todoIDArray = new ArrayList<Integer>();
				
				for(int i = 0 ; i < TODOListManager.getTODOList().size(); i++){
					todoIDArray.add((int) TODOListManager.getTODOList().get(i).getID());
				}
				
				DebugIntent.putExtra("positions", todoIDArray);
				startActivity(DebugIntent);
				//Intent intent = new Intent(TODOListView.this, GeoTabView.class);
				//startActivity(intent);
				break;

			case R.id.MENU_INFO:
				StringBuffer infoString = new StringBuffer();
				infoString.append("Information\n");
				infoString.append("Update Time : " + GeoAlarmSetting.getInstance(context).getServiceUpdateTime());
				infoString.append("\n");
				infoString.append("Service Bound Count : "+mService.getServiceBoundCount());
				infoString.append("\n");
				//infoString.append("Provider Enabled? : "+ mService.isProviderEnabled());
				infoString.append("\n");
				//infoString.append("Provider : "+ mService.getProvider());
				infoString.append("\n");
				infoString.append("checkCount : "+mService.getCheckCount());
				//infoString.append("Best Provider : "+ mService.getBestProvider());
				
				//infoString.append("Current Location"+mService.getMyLocation().getLatitude()+","+mService.getMyLocation().getLongitude());
				Toast.makeText(context, infoString.toString(), 1).show();
				break;
			*/
			default:
				break;
		}
		
		return result;
	}

	public void goToNewToDoActivity(){
    	Intent intent = new Intent(this, TODONew.class);
    	startActivityForResult(intent,1);
    }

    public void setCurrentTab(int tabNubmer){
    	if(tabNubmer > mTabHost.getChildCount()){
    		return;
    	}
    	mTabHost.setCurrentTab(tabNubmer);
    }

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		Log.i("GeoAlarm", "Destroy");
		// TODO Unbind Service here
		unbindService(mConnection);
		super.onDestroy();
	}
    
    
    
}

